package com.whx.controller;

import com.whx.pojo.Result;
import com.whx.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        // 把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        // 防止文件覆盖
        assert originalFilename != null;
        String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        /*file.transferTo(new File("C:\\Users\\86191\\Desktop\\workspace\\practice\\files\\" + fileName));*/
        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());
        return Result.success(url);
    }

}
